package development;

import java.util.Vector;

import state.AlertMenu;
import state.GameConstants;
import state.GameControl;
import state.GameForm;
import state.GameGlobal;
import state.GameImage;
import state.Graphics;
import state.ResourceManager;
import state.component.Event;
import state.component.EventListener;
import state.component.Style;
import state.component.TextFieldStyle;
import state.font.FontManager;
import state.font.ImageFont;
import state.socket.ClientConnection;
import state.socket.DataPackage;
import state.util.GameColor;
import state.util.GameStringUtils;
import state.util.Hashtable;
import state.util.ImageUtil;
import state.util.NumberUtil;
import state.util.ResourceFinder;
import state.util.TwoDimensionInt;
import state.util.TwoDimensionString;
import state.util.array.IntArray;
import state.util.array.StringArray;
import development.app.AppBussiness;
import development.bean.ChatRoom;
import development.bean.Game;
import development.bean.LoginInfo;
import development.bean.Provider;
import development.bean.Room;
import development.bean.Service;
import development.bean.Session;
import development.bean.Table;
import development.bean.User;
import development.game.DaoVangMap;
import development.group.FrmGroupMenu;
import development.socket.AppHandle;
import development.socket.AuthenticateHandle;
import development.socket.ChatHandle;
import development.socket.GameActionHandle;
import development.socket.ProtocolConstants;
import development.socket.SocketClientUtil;

public class Global extends GameGlobal {
  public static AuthenticateHandle authenticateHandle;
  public static GameActionHandle gameActionHandle;
  public static ChatHandle chatHandle;
  public static AppHandle appHandle;

  public static Hashtable resourceConfig = new Hashtable();
  public static Hashtable smsCommand = new Hashtable();
  public static Hashtable smsServiceCommand = new Hashtable();
  
  public static boolean isSystemInitDone = false;
  public static boolean isNeedToUpdate = false;
  
  public static StringArray gameToReject;
  public static StringArray appToReject;
  
  public static void initBeforeStart() {
    loadConfig();
    
    provider = Rms.loadProvider();
    loginInfo = Rms.loadLoginInfo();
    // neu la provider_id cua tinh van thi load them config
    if(provider.getId() == Constants.TINHVAN_PROVIDER_ID || provider.getId() == Constants.HDC_PROVIDER_ID)
      loadXASConfig();
    
    initResouceProfileForGame();
    DaoVangMap.initDaoVangMap();
    
    ResourceManager.setResourceFinder(new ResourceFinder() {
      public void requestResource(String category, String name) {
        SocketClientUtil.requestImage(category, name);
        if (!Constants.IS_240x320_SCREEN && (name != null) && !name.endsWith("_320240.png") && name.endsWith(".png")) {
          SocketClientUtil.requestImage(category, name.substring(0, name.indexOf(".png")) + "_320240.png");
        }
      }
    });
    
    authenticateHandle = new AuthenticateHandle();
    gameActionHandle = new GameActionHandle();
    chatHandle = new ChatHandle();
    appHandle = new AppHandle();
  }

  public static void init() {
    TIME_OUT = 60 * 60 * 1000;
    SocketClientUtil.connectToServerForAuthenticate(Global.authenticateHandle);
    initGameList();
    initStyle();
    for (int i = 0; i < Constants.GAME_ID.len(); i++) {
      Game game = new Game(Constants.GAME_ID.get(i));
      game.setName(Constants.GAME_NAME.get(i));
      Global.gameMap.put(game.getId(), game);
    }
    
    systemCanvas.setOutOfMemoryListener(new EventListener() {
      public void onActionPerform(Event event) {
        if (Global.authenClient != null && Global.authenClient.isConnectSuccess) {
          Global.authenClient.write(DataPackage.createDataPackage1(ProtocolConstants.RequestHeader.OUT_OF_MEMORY_NOTIFY_REQUEST));
        }
      }
    });
    isSystemInitDone = true;
    GameGlobal.nextState(FrmDownloadResource.instance(), null);
    new GameControl() {
      public void perform() {
        GameGlobal.nextState(FrmLogin.instance(), null);
      }
    }.setTimeInterval(100).start();
  }
  
  public static void initStyle() {
    // Menu style
    Constants.MENU_STYLE.setBorderColor(GameColor.WHITE_CODE);
    Constants.MENU_STYLE.setItemBackgroundImage(DrawUtils.getMenuItemBackground(120));
    Constants.MENU_STYLE.setForceGroundColor(GameColor.WHITE_CODE);
    Constants.MENU_STYLE.setFocusFgColor(0x7F0300);
    Constants.MENU_STYLE.setItemHeight(20);
    Constants.MENU_STYLE.setItemWidth(120);
    Constants.MENU_STYLE.setFont(FontManager.getFont(FontManager.FONT_SIZE_8));
    Constants.MENU_STYLE.setTextDx(5);
    Constants.MENU_STYLE.setTextDy(2);
    
    // Text field style
    Constants.TEXT_FIELD_STYLE.setBackgroundColor(GameColor.WHITE_CODE);
    Constants.TEXT_FIELD_STYLE.setHasBorder(false);
    Constants.TEXT_FIELD_STYLE.setForceGroundColor(GameColor.BLACK_CODE);
    Constants.TEXT_FIELD_STYLE.setFont(FontManager.getFont(FontManager.FONT_SIZE_11));
    Constants.TEXT_FIELD_STYLE.setAlign(TextFieldStyle.ALIGN_LEFT);
    Constants.TEXT_FIELD_STYLE.setFocusable(true);
    Constants.TEXT_FIELD_STYLE.setFocusBgColor(0xFDFBB7);
    Constants.TEXT_FIELD_STYLE.setFocusFgColor(GameColor.BLACK_CODE);
    
    // Chat Text field style
    Constants.CHAT_TEXT_FIELD_STYLE.setFillBackGround(true);
    Constants.CHAT_TEXT_FIELD_STYLE.setBackgroundColor(GameColor.WHITE_CODE);
    Constants.CHAT_TEXT_FIELD_STYLE.setBorderColor(GameColor.WHITE_CODE);
    Constants.CHAT_TEXT_FIELD_STYLE.setFocusFgColor(GameColor.WHITE_CODE);
    Constants.CHAT_TEXT_FIELD_STYLE.setFont(FontManager.getFont(FontManager.FONT_SIZE_11));
    Constants.CHAT_TEXT_FIELD_STYLE.setAlign(TextFieldStyle.ALIGN_LEFT);
    Constants.CHAT_TEXT_FIELD_STYLE.setFocusable(true);
    Constants.CHAT_TEXT_FIELD_STYLE.setFocusBgColor(GameColor.BLACK_CODE);
    Constants.CHAT_TEXT_FIELD_STYLE.setFocusFgColor(GameColor.WHITE_CODE);
    
    // Checkbox style
    Constants.CHECK_BOX_STYLE.setHasBorder(true);
    Constants.CHECK_BOX_STYLE.setBorderColor(GameColor.WHITE_CODE);
    Constants.CHECK_BOX_STYLE.setFocusable(true);
    Constants.CHECK_BOX_STYLE.setForceGroundColor(GameColor.WHITE_CODE);
    Constants.CHECK_BOX_STYLE.setFocusFgColor(GameColor.WHITE_CODE);
    Constants.CHECK_BOX_STYLE.setFillBackGround(false);
    
    Constants.CHECK_BOX_STYLE_APP.setHasBorder(true);
    Constants.CHECK_BOX_STYLE_APP.setBorderColor(GameColor.YELLOW_CODE);
    Constants.CHECK_BOX_STYLE_APP.setFocusable(true);
    Constants.CHECK_BOX_STYLE_APP.setForceGroundColor(GameColor.YELLOW_CODE);
    Constants.CHECK_BOX_STYLE_APP.setFocusFgColor(GameColor.WHITE_CODE);
    Constants.CHECK_BOX_STYLE_APP.setFillBackGround(false);
    Constants.CHECK_BOX_STYLE_APP.setFocusBgColor(GameColor.WHITE_CODE);
    
    Constants.TEXT_FIELD_STYLE_APP.setBorderColor(GameColor.YELLOW_CODE);
    Constants.TEXT_FIELD_STYLE_APP.setBackgroundColor(GameColor.WHITE_CODE);
    Constants.TEXT_FIELD_STYLE_APP.setHasBorder(true);
    Constants.TEXT_FIELD_STYLE_APP.setForceGroundColor(GameColor.WHITE_CODE);
    Constants.TEXT_FIELD_STYLE_APP.setFont(FontManager.getFont(FontManager.FONT_SIZE_11));
    Constants.TEXT_FIELD_STYLE_APP.setAlign(TextFieldStyle.ALIGN_LEFT);
    Constants.TEXT_FIELD_STYLE_APP.setFocusable(true);
    Constants.TEXT_FIELD_STYLE_APP.setFocusBgColor(GameColor.WHITE_CODE);
    
    // Multi tab style
    Constants.MULTI_TAB_STYLE.setBorderColor(GameColor.WHITE_CODE);
    Constants.MULTI_TAB_STYLE.setForceGroundColor(GameColor.WHITE_CODE);
    Constants.MULTI_TAB_STYLE.setFocusFgColor(GameColor.YELLOW_CODE);
    Constants.MULTI_TAB_STYLE.setFocused(true);
    
    // Alert menu style
    Style ALERT_MENU_STYLE = new Style();
    ALERT_MENU_STYLE.setBackgroundColor(0xD0A736);
    ALERT_MENU_STYLE.setBorderColor(GameColor.WHITE_CODE);
    ALERT_MENU_STYLE.setForceGroundColor(GameColor.WHITE_CODE);
    ALERT_MENU_STYLE.setFocusFgColor(0x7F0300);
    ALERT_MENU_STYLE.setFocusBgColor(GameColor.GREEN_CODE);
    ALERT_MENU_STYLE.setFont(FontManager.getFont(FontManager.FONT_SIZE_8));
    AlertMenu.setStyle(ALERT_MENU_STYLE);
  }
  
  public static int getGameIndexById(String gameId) {
    for (int i = 0; i < Constants.GAME_ID.len(); i++) {
      if (Constants.GAME_ID.get(i).equals(gameId)) {
        return i;
      }
    }
    System.out.println("Global : getGameIndexById : wrong gameId: " + gameId);
    return 0;
  }
  
  public static void initGameList() {
    // Declare for GAME
    Constants.GAME_NAME = new StringArray(new String[] {  "Đào vàng",            "ALTP",                     "Audition",          "DHBC",                       "TLMN",                   "Xì tố",            "Phỏm",             "Ba cây",            "TLMB",                  "Cờ tướng",           "Caro",              "Cờ vua"            }, 12);
    Constants.GAME_ID = new StringArray(new String[] {    "daovang",             "ailatrieuphu",             "audition",          "duoihinhbatchu",             "tlmn",                   "xito",             "tala",             "bacay",             "tlmb",                  "cotuong",            "caro",              "covua"             }, 12);
    Constants.GAME_ICON = new StringArray(new String[]   {"8.Dao-vang.png",      "10.Ai-la-trieu-phu.png",   "13.Audition.png",   "11.Duoi-hinh-bat-chu.png",   "5.Tien-len-MN.png",      "7.Xi-to.png",      "1.Phom.png",       "6.Ba-cay.png",      "2.Tien-len-MB.png",    "3.Co-tuong.png",     "4.Co-caro.png",     "12.Co-vua.png"     }, 12);
    Constants.GAME_USER_COUNT = IntArray.create(Constants.GAME_ID.len());
    
    Constants.GAME_TOOLTIP = new TwoDimensionString(2, 12);
    Constants.GAME_TOOLTIP.setColumn(0, new StringArray(new String[] {"Đào vàng", "Ai là",                   "Audition",            "Đuổi hình",                 "Tiến lên",               "Xì tố",           "Phỏm",              "Ba cây",           "Tiến lên",              "Cờ tướng",           "Cờ carô",            "Cờ vua"            }, 12));
    Constants.GAME_TOOLTIP.setColumn(1, new StringArray(new String[] {"",         "triệu phú",               "",                    "bắt chữ",                   "Miền Nam",               "",                 "",                 "",                 "Miền Bắc",              "",                    "",                   ""            }, 12));
    
    int deleteSuccessCount = 0;
    int index = 0;
f1: while (index < Constants.GAME_ID.len()) {
      for (int j = 0; j < gameToReject.len(); j++) {
        if (Constants.GAME_ID.get(index).equalsIgnoreCase(gameToReject.get(j).trim())) {
          GameStringUtils.deleteElementAtStringArray(Constants.GAME_NAME, index);
          GameStringUtils.deleteElementAtStringArray(Constants.GAME_ID, index);
          GameStringUtils.deleteElementAtTwoDimensionStringArray(Constants.GAME_TOOLTIP, index);
          GameStringUtils.deleteElementAtStringArray(Constants.GAME_ICON, index);
          GameStringUtils.deleteElementAt(Constants.GAME_USER_COUNT, index);
          deleteSuccessCount++;
          continue f1;
        }
      }
      index++;
    }
    Constants.GAME_NAME = GameStringUtils.subArrayString(Constants.GAME_NAME, 0, Constants.GAME_NAME.len() - deleteSuccessCount);
    Constants.GAME_ID = GameStringUtils.subArrayString(Constants.GAME_ID, 0, Constants.GAME_ID.len() - deleteSuccessCount);
    Constants.GAME_TOOLTIP = GameStringUtils.subTwoDimensionStringArray(Constants.GAME_TOOLTIP, 0, Constants.GAME_TOOLTIP.getWidth() - deleteSuccessCount);
    Constants.GAME_ICON = GameStringUtils.subArrayString(Constants.GAME_ICON, 0, Constants.GAME_ICON.len() - deleteSuccessCount);
    Constants.GAME_USER_COUNT = GameStringUtils.subArray(Constants.GAME_USER_COUNT, 0, Constants.GAME_USER_COUNT.len() - deleteSuccessCount);
  }
  
  private static void initResouceProfileForGame() {
    resourceConfig.put(Constants.INIT_RESOURCE_PROFILE, new String[]{Constants.MANDATORY_CATEGORY, Constants.TABLE_5_PLAYER_IMAGE_CATEGORY, Constants.TABLE_6_PLAYER_IMAGE_CATEGORY});
    
    // Game Resource
    resourceConfig.put("daovang", new String[]{Constants.DAO_VANG_IMAGE_CATEGORY, Constants.HIEU_UNG_IMAGE_CATEGORY});
    resourceConfig.put("bacay", new String[]{Constants.CARD_IMAGE_CATEGORY, Constants.BA_CAY_IMAGE_CATEGORY, Constants.TABLE_6_PLAYER_IMAGE_CATEGORY});
    resourceConfig.put("tala", new String[]{Constants.CARD_IMAGE_CATEGORY});
    resourceConfig.put("xito", new String[]{Constants.CARD_IMAGE_CATEGORY, Constants.XI_TO_IMAGE_CATEGORY, Constants.TABLE_5_PLAYER_IMAGE_CATEGORY});
    resourceConfig.put("tlmn", new String[]{Constants.CARD_IMAGE_CATEGORY});
    resourceConfig.put("tlmb", new String[]{Constants.CARD_IMAGE_CATEGORY});
    resourceConfig.put("cotuong", new String[] {Constants.CO_TUONG_IMAGE_CATEGORY, Constants.HIEU_UNG_IMAGE_CATEGORY});
    resourceConfig.put("covua", new String[] {Constants.CO_VUA_IMAGE_CATEGORY, Constants.HIEU_UNG_IMAGE_CATEGORY});
    resourceConfig.put("caro", new String[] {Constants.HIEU_UNG_IMAGE_CATEGORY});
    resourceConfig.put("ailatrieuphu", new String[] {Constants.AI_LA_TRIEU_PHU_IMAGE_CATEGORY, Constants.TABLE_6_PLAYER_IMAGE_CATEGORY});
    resourceConfig.put("duoihinhbatchu", new String[] {Constants.DUOI_HINH_BAT_CHU_IMAGE_CATEGORY, Constants.TABLE_6_PLAYER_IMAGE_CATEGORY});
    
    // Put audition resource
    // TODO: disable game Audition
//    String[] auditionDefaultResource = null;
//    String[] allResousrceForAudition = null;
//    if (Constants.IS_240x320_SCREEN) {
//      auditionDefaultResource = new String[]{Constants.AUDITION_IMAGE_CATEGORY, Constants.AUDITION_EXTENSION_CATEGORY, Constants.AUDITION_LAZY_CATEGORY, Constants.HIEU_UNG_IMAGE_CATEGORY, "aloha"};
//    } else {
//      auditionDefaultResource = new String[]{Constants.AUDITION_320x240_IMAGE_CATEGORY, Constants.AUDITION_EXTENSION_CATEGORY, Constants.AUDITION_LAZY_CATEGORY, Constants.HIEU_UNG_IMAGE_CATEGORY, "aloha"};
//    }
//    allResousrceForAudition = new String[auditionDefaultResource.length + FrmSelectSong.SONG_ID_ARRAY.length];
//    ByteArray.arraycopy(auditionDefaultResource, 0, allResousrceForAudition, 0, auditionDefaultResource.length);
//    ByteArray.arraycopy(FrmSelectSong.SONG_ID_ARRAY, 0, allResousrceForAudition, auditionDefaultResource.length, FrmSelectSong.SONG_ID_ARRAY.length);
//    resourceConfig.put("audition", allResousrceForAudition);
    
    // App resource
    resourceConfig.put("xoso", new String[] {Constants.XO_SO_CATEGORY,Constants.KET_BAN_CATEGORY});
    resourceConfig.put("thoitiet", new String[] {Constants.THOI_TIET_CATEGORY,Constants.KET_BAN_CATEGORY});
    resourceConfig.put("ketban", new String[] {Constants.KET_BAN_CATEGORY});
    resourceConfig.put("camnang", new String[] {Constants.CAM_NANG_CATEGORY,Constants.KET_BAN_CATEGORY});
    resourceConfig.put("lichth", new String[] {Constants.LTH_CATEGORY,Constants.KET_BAN_CATEGORY});
    resourceConfig.put("mangxh", new String[] {Constants.MANGXH_CATEGORY});
    resourceConfig.put("chat", new String[] {Constants.CHAT_CATEGORY});
  }
  
  public static int HEART_BREATH_SEQUENCE_TIME = 1000;

  public static ClientConnection authenClient;
  public static ClientConnection gameActionClient;
  public static ClientConnection chatClient;
  public static ClientConnection appClient;

  public static Provider provider;
  public static LoginInfo loginInfo;
  public static Session session;

  public static Service chatService;
  public static Service appService;
  public static Vector gameServices = new Vector();
  public static Hashtable gameMap = new Hashtable();
  public static Vector tableUsers = new Vector();
  public static Vector friends = new Vector();
  public static Vector freeUser = new Vector();
  public static Vector requestFriendData = new Vector();
  public static Hashtable chatRoomMap = new Hashtable();
  public static Hashtable userCachedMap = new Hashtable();
  
  public static Hashtable subscriptionMap = new Hashtable();
  
  public static Vector topRickest = new Vector();
  public static Vector topLevel = new Vector();

  public static User currentUser = new User();
  public static Game currentGame;
  public static AppBussiness currentApp;
  public static Room currentRoom;
  public static Table currentTable;
  public static ChatRoom currentChatRoom;
  public static Service currentGameService;

  public static String waitingToValidUsername; // Username mà người dùng sử dụng để đăng nhập
  public static String validedUsername; // Username mà đã xác nhận ngầm
  public static String downloadUrl;
  public static int numberOfUserOnline = -1;
  
  public static ChatComponent chatComponent;
  
  private static GameImage defaultAvatar;

  public static GameImage getAvatar(String name) {
    User user = (User) userCachedMap.get(name);
    if (user == null) {
      SocketClientUtil.requestUserInfo(name);
      return getDefaultAvatar();
    }
    return getAvatarByAvatarId(name, user.getAvatarId());
  }
  
  public static GameImage getAvatarByAvatarId(String username, String avatarId) {
    GameImage avatar = AvatarCache.getAvatar(avatarId);
    if (avatar == null) {
      SocketClientUtil.getAvatar(username, avatarId);
      GameImage defaultAvatar1 = getDefaultAvatar();
      AvatarCache.putAvatar(avatarId, defaultAvatar1);
      return defaultAvatar1;
    }
    return avatar;
  }
  
  public static GameImage getDefaultAvatar() {
    if (defaultAvatar == null) {
      defaultAvatar = ImageUtil.getImage(Constants.NEW_IMAGE_CATEGORY, "default_avatar.png");
      defaultAvatar.setStatic(true);
    }
    return defaultAvatar;
  }
  
  
  /**************************CHAT FUNCTION*****************************/
  public static  void initChatComponent(TwoDimensionInt chatLocation240x320, TwoDimensionInt chatLocation320x240) {
    if (Global.chatComponent == null) {
      return;
    }
    Global.chatComponent.init();
    if (Constants.IS_240x320_SCREEN) {
      Global.chatComponent.initChatLocation(chatLocation240x320);
    } else {
      Global.chatComponent.initChatLocation(chatLocation320x240);
    }
    ImageFont text8 = FontManager.getFont(FontManager.FONT_SIZE_8);
    Global.chatComponent.setInputChatMessageTextField(Constants.CHAT_TEXT_FIELD_STYLE, 5, GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight() - text8.getHeight() - 5, GameConstants.SCREEN_WIDTH - 10, text8.getHeight());
  }
  
  public static boolean isShowedInputChatBox() {
    return (Global.chatComponent != null) && Global.chatComponent.isShowInputChatMessageTextField();
  }
  
  public static void hideInputChatBox() {
    if (Global.chatComponent != null) {
      Global.chatComponent.setShowInputChatMessageTextField(false);
    }
  }

  public static void drawChat(Graphics g) {
    if (Global.chatComponent == null) {
      return;
    }
    
    Global.chatComponent.draw(g);
    if (Global.chatComponent.isShowInputChatMessageTextField()) {
      DrawUtils.setBottomBar(Global.systemCanvas.frmCurrent.getManager(), "Trở về", "Gửi", "Xóa");
    }
  }
  
  public static void loadConfig() {
    gameToReject = StringArray.create(0);
    String guideContent = Config.config;
    StringArray configs = GameStringUtils.split(guideContent, "\n");
    for (int i = 0; i < configs.len(); i++) {
      StringArray vals = GameStringUtils.split(configs.get(i).trim(), "=");
      if (vals.get(0).trim().equals("MODE")) {
        if (vals.len() > 0) {
          String str = vals.get(1).trim().toLowerCase();
          if (str.equalsIgnoreCase("debug")) {
            Global.systemCanvas.setDebugMode(true);
          }
        }
      } else if (vals.get(0).trim().equals("DEFAULT_PROVIDER_ID")) {
        Constants.DEFAULT_PROVIDER_ID = NumberUtil.parseInt(vals.get(1).trim());
      } else if (vals.get(0).trim().equals("GAME_VERSION_INFO")) {
        Constants.GAME_VERSION_INFO = vals.get(1).trim();
      } else if (vals.get(0).trim().equals("DISABLE_GAME")) {
        gameToReject = GameStringUtils.split(vals.get(1), ",");
      } else if (vals.get(0).trim().equals("DISABLE_APP")) {
        appToReject = GameStringUtils.split(vals.get(1), ",");
      }
    }
  }
  public static void loadXASConfig() {
    String strXAS = XasConfig.config;
    StringArray configs = GameStringUtils.split(strXAS, "\n");

    for (int i = 0; i < configs.len(); i++) {
      if (configs.get(i).trim().startsWith("CARD_URL=")) {
        Constants.CARD_URL = GameStringUtils.replaceAll(configs.get(i).trim(), "CARD_URL=", "");
      } else if (configs.get(i).trim().startsWith("CARD_SECURE_KEY=")) {
        Constants.CARD_SECURE_KEY = GameStringUtils.replaceAll(configs.get(i).trim(), "CARD_SECURE_KEY=", "");
      } else if (configs.get(i).trim().startsWith("CARD_PROVIDERS=")) {
        Constants.CARD_PROVIDERS = GameStringUtils.replaceAll(configs.get(i).trim(), "CARD_PROVIDERS=", "");
      } else if (configs.get(i).trim().startsWith("SMS_PREFIX=")) {
        Constants.SMS_PREFIX = GameStringUtils.replaceAll(configs.get(i).trim(), "SMS_PREFIX=", "");
      } else if (configs.get(i).trim().startsWith("SMS_NUMBERS=")) {
        Constants.SMS_NUMBERS = GameStringUtils.replaceAll(configs.get(i).trim(), "SMS_NUMBERS=", "");
      } else if (configs.get(i).trim().startsWith("HASH=")) {
        Constants.HASH = GameStringUtils.replaceAll(configs.get(i).trim(), "HASH=", "");
      }
    }
  }
  
  public static void goBack(GameForm backForm) {
    if (backForm != null) {
      Global.nextState(backForm, null);
    } else {
      if (Global.systemCanvas.lastState instanceof FrmDownloadResource) {
        GameGlobal.nextState(FrmGroupMenu.instance(), null);
      } else {
        GameGlobal.nextState(Global.systemCanvas.lastState, null);
      }
    }
  }
  
  public static void checkToDisplayAddFriendRequest() {
    new GameControl() {
      public void perform() {
        Global.authenticateHandle.friendBussiness.displayNextAddFriendAlert();
      }
    }.setTimeInterval(500).start();
  }
}
